package com.bawei.module_launcher.gateway

import android.view.LayoutInflater
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_mvi.ui.BaseMviLazyFragment
import com.bawei.module_launcher.R
import com.bawei.module_launcher.databinding.LauncherFragmentGateWayBinding
import com.bw.quickadapter.BaseQuickMultipleAdapter
import com.umeng.analytics.MobclickAgent
import kotlinx.coroutines.launch

class GateWayFragment : BaseMviLazyFragment<LauncherFragmentGateWayBinding>() {
    private lateinit var viewModel:GateWayViewModel

    private lateinit var gateWayAdapter: GateWayAdapter
    //4：根据状态做UI更新
    override fun handleState() {
      lifecycleScope.launch {
          viewModel.state.collect{
              when(it){
                  is GateWayState.GateWaySuccessState ->
                  {
                      showMsg("请求成功 ${it.list.size}")
                      gateWayAdapter.setNewInstance(it.list)//请求数据刷新适配器展示UI
                  }
                  is GateWayState.Init ->{}
              }
          }
      }
    }
    //手动采集fragment页面
    override fun onResume() {
        super.onResume()
        MobclickAgent.onPageStart("GateWayFragment");
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPageEnd("GateWayFragment");
    }

    override fun getLayout(): Int {
        return R.layout.launcher_fragment_gate_way

    }
    //加载门户页面UI
    override fun initView() {
        super.initView()
        gateWayAdapter = GateWayAdapter()
        //添加尾布局
        val foot1View = LayoutInflater.from(context).inflate(R.layout.layout_official,null,false)
        val foot2View = LayoutInflater.from(context).inflate(R.layout.layout_knowledge,null,false)
        gateWayAdapter.setFoodView(foot1View)
        gateWayAdapter.setFoodView(foot2View)

        val manager = GridLayoutManager(context,3)
        manager.orientation = GridLayoutManager.VERTICAL
        viewDataBinding.gatewayRv.layoutManager = manager
        //注意！！！！ 适配器最后设置
        viewDataBinding.gatewayRv.adapter = gateWayAdapter
        //点击item跳转到不同模块中页面
        gateWayAdapter.setOnItemCLickListener(object :BaseQuickMultipleAdapter.OnItemCLickListener{
            override fun onItemClick(adapter: RecyclerView.Adapter<*>?, position: Int) {
                if(position == 1){//在线应用
                    ARouter.getInstance().build("/apps/net").navigation()
                    //采集并传参数
                    MobclickAgent.onEvent(context, "net_app")
                }else if(position == 2){//本地应用
                    ARouter.getInstance().build("/apps/local").navigation()
                    //采集的是点击事件
                    MobclickAgent.onEvent(context,"local_app")
                }else if(position == 7){//文件管理
                    ARouter.getInstance().build("/documents/DocumentsActivity").navigation()
                }else if(position == 5){//日志管理
                    ARouter.getInstance().build("/journal/JournalActivity").navigation()
                }else if(position == 3){//签到
                    ARouter.getInstance().build("/attendance/signactivity").navigation()

                }
            }
        })

    }


    override fun initData() {
        super.initData()
        //协程作用域:发送意图给viewmodel
        lifecycleScope.launch {
            viewModel.intents.send(GateWayIntent.DataIntent)
        }
    }


    override fun generateViewModel() {
        viewModel = ViewModelProvider(this).get(GateWayViewModel::class.java)
    }


}